﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace ShootEm.Components
{
    class Target : Movable
    {

        // Sprite element
        int spriteWidth = 156;
        int spriteHeight = 153;

        // Drawing zone
        //A rectangle to store which 'frame' is currently being shown
        Rectangle sourceRect;
        //The centre of the current 'frame'
        Vector2 origin;




        //A Timer variable
        float timer = 0f;
        //The interval (100 milliseconds)
        float interval = 100f;
        //Current frame holder (start at 1)
        int currentFrame = 1;

        public Target(ShootEm game, int x, int y, string texture, int vitesse)
            : base(game, x, y, texture, vitesse)
        {
           // this.spriteHeight = spriteHeight;
            //this.spriteWidth = spriteWidth;
        }
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            this.iterationOnTexture(gameTime);
            this.x = this.x - this.vitesse;
            if (this.x + this.spriteWidth <= 0)
            {
                this.Game.Components.Remove(this);
              // MessageBox(new IntPtr(0), "Deleted component", "Info", 0);
            }
            base.Update(gameTime);
        }

        private void iterationOnTexture(GameTime gameTime)
        {
            //Increase the timer by the number of milliseconds since update was last called
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            //Check the timer is more than the chosen interval
            if (timer > interval)
            {
                //Show the next frame
                currentFrame++;
                //Reset the timer
                timer = 0f;
            }

            if (currentFrame == 7)
            {
                currentFrame = 0;
            }

            sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
            origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        }

        public override void Draw(GameTime gameTime)
        {
           // ((ShootEm)this.Game).GraphicsDevice.Clear(Color.Beige);

            ((ShootEm)this.Game).spriteBatch.Begin();
            ((ShootEm)this.Game).spriteBatch.Draw(this.texture, new Vector2(this.x, this.y), sourceRect, Color.White, 0f, origin, 1, SpriteEffects.None, 0);
            ((ShootEm)this.Game).spriteBatch.End();


        }
    }
}
